﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using KeRui.Common;


public partial class Manage_Cms_NewsStatistics : System.Web.UI.Page
{
    //时间
    protected string begin = PageRequest.GetString("begin");
    protected string end = PageRequest.GetString("end");
    //新闻总条数
    protected int checktype = PageRequest.GetInt("checktime", -1);
    protected int total = 0;
    protected int siteid = PageRequest.GetInt("siteid", -1);
    protected DataTable dt = new DataTable();
    protected string act = PageRequest.GetString("act");
    protected void Page_Load(object sender, EventArgs e)
    {
        //检查登录
        ManageHelper.CheckAdminLogin();
        //查看权限
        ManageHelper.CheckAdminPower("site_newsstatistics_" + siteid);
        switch (act)
        {
            case "look": GetAll(); break;
            case "toexcel":GetAll();toExcel(dt);break;
            default: begin = DateTime.Now.ToShortDateString();
                end = DateTime.Now.ToString();
                break;
        }
    }
    private void GetAll()
    {
        KeRui.BLL.CmsContentForModelid BLLCmsContentForModelid = new KeRui.BLL.CmsContentForModelid();
        string timetye="publishtime";
        if (checktype==2)
        {
            timetye="UpdateTime";
        }
        string strWhere = "contentid in (select contentid from cmscontent where " + timetye + " BETWEEN '" + begin + "' and dateadd(day,1,'" + begin + "')) group by Editor ";
        DataSet ds = BLLCmsContentForModelid.GetDataTableBySql("Editor,COUNT(1) AS c ", " c DESC", "CmsContent0", strWhere);
        dt = ds.Tables[0];
        int numb = 0;
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            numb += Convert.ToInt32(dt.Rows[i]["c"]);
        }
        total = numb ;
    }
    protected string  getnum(int num)
    {
        double Percent = 100 * (double.Parse(num.ToString()) / double.Parse(total.ToString()));
        return Percent.ToString("n2") + "%";
    }
    protected int getPercent(int num)
    {
        double Percent = (double.Parse(num.ToString()) / double.Parse(total.ToString()));
        return Convert.ToInt32(Percent * 495) + 5;
    }

    //导出数据到EXCEL
    protected void toExcel(DataTable dt)
    {
        string downdata = "";
        downdata += "<table  cellspacing='0' cellpadding='4' id='DataGrid' style='width:680px' border='1' >";
        
        downdata += "<tr><th colspan=3><center><b>新闻量统计（" + begin + "）</b></center></th></tr>";
        downdata += "<tr>";
        downdata += "<th class=first-cell>作者</th>";
        downdata += "<th>新闻量</th>";
        downdata += "</tr>";
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            downdata += "<tr>";

            downdata += "<td class=first-cell>";
            downdata += dt.Rows[i][0].ToString();
            downdata += "</td>";

            downdata += "<td>";
            downdata += dt.Rows[i][1].ToString();
            downdata += "</td>";

            downdata += "</tr>";
        }
        downdata += "<table>";



        downxls(downdata, "新闻量统计（" + begin + "）");
    }
    //导出方法
    void downxls(string xl, string filename)
    {
        Response.Clear();
        Response.Buffer = true;
        Response.Charset = "UTF-8";
        Response.ContentEncoding = System.Text.Encoding.UTF8;
        //Response.ContentEncoding = System.Text.Encoding.UTF8; 
        Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", Utils.UrlEncode(filename,"UTF-8") + ".xls"));
        //Response.Charset = "UTF-8";
        Response.ContentType = "application/ms-excel";
        Response.Write(xl);
        Response.End();
    }
}
